package com.ruoyi.orderEat.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.orderEat.domain.*;
import com.ruoyi.orderEat.mapper.ShopFlavourMapper;
import com.ruoyi.orderEat.mapper.ShopSpecificationMapper;
import com.ruoyi.orderEat.mapper.SpecificationAssociationMapper;
import com.ruoyi.orderEat.service.IShopFlavourService;
import org.apache.http.HttpStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品口味Service业务层处理
 *
 * @author DingHao
 * @date 2024-01-12
 */
@Service
public class ShopFlavourServiceImpl implements IShopFlavourService {
    @Autowired
    private ShopFlavourMapper shopFlavourMapper;

    @Autowired
    private ShopSpecificationMapper shopSpecificationMapper;

    @Autowired
    private SpecificationAssociationMapper specificationAssociationMapper;


    /**
     * 查询商品口味
     *
     * @param flavourId 商品口味主键
     * @return 商品口味
     */
    @Override
    public ShopFlavour selectShopFlavourByFlavourId(Long flavourId) {
        return shopFlavourMapper.selectShopFlavourByFlavourId(flavourId);
    }

    /**
     * 查询商品口味列表
     *
     * @param shopFlavourDTO 商品口味
     * @return 商品口味
     */
    @Override
    public List<ShopFlavourVO> selectShopFlavourList(ShopFlavourDTO shopFlavourDTO) {


        return shopFlavourMapper.selectShopFlavourList(shopFlavourDTO);
    }

    @Override
    public List<ShopFlavour> queryShopFlavourList(Long specificationId) {

        List<ShopFlavour> shopFlavours = shopFlavourMapper.selectFlavourName(specificationId);
        return shopFlavours;
    }

    /**
     * 新增商品口味
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int insertShopFlavour(ShopFlavourDTO shopFlavourDTO) {

        Long specificationId = shopFlavourDTO.getSpecificationId();
        if (specificationId!=null){
            createFlavour(shopFlavourDTO,specificationId);
        }else {
            ShopSpecification shopSpecification = new ShopSpecification();
            BeanUtils.copyProperties(shopFlavourDTO, shopSpecification);
            shopSpecification.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            shopSpecification.setCreateTime(new Date());
            shopSpecificationMapper.insert(shopSpecification);
            Long newSpecificationId = shopSpecification.getSpecificationId();
            createFlavour(shopFlavourDTO,newSpecificationId);
        }
        return 1;
    }

    @Override
    public void associationShopFlavour(ShopFlavourDTO shopFlavourDTO) {
        List<Long> flavourIds = shopFlavourDTO.getFlavourIds();
        Long shopId = shopFlavourDTO.getShopId();
        Long specificationId = shopFlavourDTO.getSpecificationId();
        Long userId = SecurityUtils.getUserId();
        List<SpecificationAssociation> specificationAssociationList = new ArrayList<>();
        for (Long flavourId : flavourIds) {
            List<SpecificationAssociation> specificationAssociations = specificationAssociationMapper.selectAllByShopIdAndSpecificationIdAndFlavourId (shopId, specificationId, flavourId);
            if (!specificationAssociations.isEmpty()) {
                throw new ServiceException("此商品的规格名称重复", HttpStatus.SC_BAD_REQUEST);
            }
            SpecificationAssociation specificationAssociation = new SpecificationAssociation();
            specificationAssociation.setFlavourId(flavourId);
            specificationAssociation.setSpecificationId(specificationId);
            specificationAssociation.setShopId(shopId);
            specificationAssociation.setCreateBy(String.valueOf(userId));
            specificationAssociation.setCreateTime(new Date());
            specificationAssociationList.add(specificationAssociation);
        }
        specificationAssociationMapper.insertBatch(specificationAssociationList);

    }

    private void createFlavour(ShopFlavourDTO shopFlavourDTO,Long specificationId){
        Long shopId = shopFlavourDTO.getShopId();
        String flavourName = shopFlavourDTO.getFlavourName();
        ShopFlavour flavour = shopFlavourMapper.selectByFlavourName(flavourName);
        if(flavour==null){
            ShopFlavour shopFlavour = new ShopFlavour();
            BeanUtils.copyProperties(shopFlavourDTO, shopFlavour);
            shopFlavour.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            shopFlavour.setCreateTime(new Date());
            shopFlavourMapper.insertShopFlavour(shopFlavour);
            Long flavourId = shopFlavour.getFlavourId();
            SpecificationAssociation specificationAssociation = new SpecificationAssociation();
            specificationAssociation.setShopId(shopId);
            specificationAssociation.setSpecificationId(specificationId);
            specificationAssociation.setFlavourId(flavourId);
            specificationAssociation.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            specificationAssociation.setCreateTime(new Date());
            specificationAssociationMapper.insert(specificationAssociation);
        }else {
            Long flavourId = flavour.getFlavourId();
            SpecificationAssociation specificationAssociation = new SpecificationAssociation();
            specificationAssociation.setShopId(shopId);
            specificationAssociation.setSpecificationId(specificationId);
            specificationAssociation.setFlavourId(flavourId);
            specificationAssociation.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            specificationAssociation.setCreateTime(new Date());
            specificationAssociationMapper.insert(specificationAssociation);
        }
    }


    /**
     * 修改商品口味
     *
     * @param shopFlavourDTO 商品口味
     * @return 结果
     */
    @Override
    @Transactional
    public int updateShopFlavour(ShopFlavourDTO shopFlavourDTO) {
        Long id = shopFlavourDTO.getId();
        specificationAssociationMapper.deleteByPrimaryKey(id);
        List<SpecificationAssociation> specificationAssociationList = specificationAssociationMapper.selectAllByShopIdAndSpecificationIdAndFlavourId (shopFlavourDTO.getShopId(), shopFlavourDTO.getSpecificationId(), shopFlavourDTO.getFlavourId());
        if (!specificationAssociationList.isEmpty()) {
            throw new ServiceException("规格名称重复", HttpStatus.SC_BAD_REQUEST);
        }
        SpecificationAssociation specificationAssociation = new SpecificationAssociation();
        specificationAssociation.setShopId(shopFlavourDTO.getShopId());
        specificationAssociation.setSpecificationId(shopFlavourDTO.getSpecificationId());
        specificationAssociation.setFlavourId(shopFlavourDTO.getFlavourId());
        specificationAssociation.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        specificationAssociation.setCreateTime(new Date());
        return specificationAssociationMapper.insert(specificationAssociation);
    }

    /**
     * 批量删除商品口味
     *
     * @param delIds@return 结果
     */
    @Override
    @Transactional
    public int deleteShopFlavourByFlavourIds(List<Integer> delIds) {
        //先删除specificationAssociation里的数据，再查询删除数据的flavourId在specificationAssociation还有没有了，如果没有了就把shopFlavour里的也删除
        List<Long> flavourIds = delIds.stream().map(id -> {
            long lid = Long.parseLong(String.valueOf(id));
            SpecificationAssociation specificationAssociation = specificationAssociationMapper.selectByPrimaryKey(lid);
            return specificationAssociation.getFlavourId();
        }).collect(Collectors.toList());
        List<Long> ids = delIds.stream().map(id -> {
            long pid = Long.parseLong(String.valueOf(id));
            return pid;
        }).collect(Collectors.toList());

        specificationAssociationMapper.deleteBatch(ids);
        for (Long flavourId : flavourIds) {
            List<SpecificationAssociation> specificationAssociationList = specificationAssociationMapper.selectAllByFlavourId(flavourId);
            if (specificationAssociationList.isEmpty()){
                shopFlavourMapper.deleteShopFlavourByFlavourId(flavourId);
            }
        }
        return 1;
    }

    /**
     * 删除商品口味信息
     *
     * @param flavourId 商品口味主键
     * @return 结果
     */
    @Override
    public int deleteShopFlavourByFlavourId(Long flavourId) {
        return shopFlavourMapper.deleteShopFlavourByFlavourId(flavourId);
    }
}
